package com.heima.search.listener;

import com.heima.common.constants.MQConstants;
import com.heima.search.service.ArticleSeachService;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleDocumentListener {
    @Autowired
    private ArticleSeachService articleSeachService;
    /**
     * 文章上架同步
     */
    @KafkaListener(topics = MQConstants.WM_NEW_UP_ES_TOPIC)
    public void handleUp(ConsumerRecord<String,String> record){
        log.info("开始文章上架同步 {}");
        if (record != null){
            String value = record.value();
            Long articleId = Long.valueOf(value);
            articleSeachService.saveToES(articleId);
        }

    }
    /**
     * 文章下架同步
     */
    @KafkaListener(topics = MQConstants.WM_NEW_DOWN_ES_TOPIC)
    public void handleDown(ConsumerRecord<String,String> record){
        log.info("开始文章下架同步 {}");
        if(record!=null){
            String value = record.value();
            Long articleId = Long.valueOf(value);
            articleSeachService.removeFromES(articleId);
        }
    }
}
